<?php
/**
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/11/9
 * Time: 10:51
 */

namespace Home\Services;


class CrmProjectsService extends BaseService
{
    public static $table = 'db_projects';
    public static $table2 = 'db_projects_weekly';

    /**
     * 获取运营项目数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getCrmProjectsArray($field, $where)
    {
        $result = parent::getResult(self::$table, $field, $where);
        return $result;
    }

    /**
     * 获取单列数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getCrmProjects($field, $where)
    {
        $result = parent::getResultByField(self::$table, $field, $where);
        return $result;
    }

    /**
     * 返回指定字段值
     * @param $field
     * @param $where
     * @return bool
     */
    public static function getCrmProjectsByField($field, $where)
    {
        $result = parent::getResultByField(self::$table, $field, $where);
        if (!empty($result)) {
            return $result[$field];
        } else {
            return false;
        }
    }


    /**
     * 录入运营项目
     * @param $data
     * @return mixed
     */
    public static function addCrmProjects($data)
    {
        $data['add_uid'] = isset($_SESSION['dms_uid']) ? $_SESSION['dms_uid'] : $_SESSION['uid'];
        $data['add_date'] = date('Y-m-d H:i:s');
        return parent::insertData(self::$table, $data);
    }

    /**
     * 录入运营项目更新状态记录
     * @param $data
     * @return bool
     */
    public static function addCrmProjectsFlow($data)
    {
        $flow_data['project_id'] = $data['project_id'];
        $flow_data['stage'] = $data['stage'];
        $flow_data['change_date'] = isset($data['change_date']) ? $data['change_date'] : date('Y-m-d');
        $flow_data['remark'] = isset($data['remark']) ? $data['remark'] : '录入运营项目时初始状态';
        $flow_data['add_uid'] = isset($_SESSION['dms_uid']) ? $_SESSION['dms_uid'] : $_SESSION['uid'];
        $flow_data['add_date'] = date('Y-m-d H:i:s');
        $table = 'db_projects_flow';
        return parent::insertData($table, $flow_data);
    }

    /**
     * 更新运营项目数据
     * @param $data
     * @param $where
     * @return mixed
     */
    public static function updateCrmProjects($data, $where)
    {
        $result = parent::save(self::$table, $data, $where);
        return $result;
    }

    /**
     * 添加周报
     * @param $data
     */
    public static function addWeekly($data)
    {
        $data['tswk'] = str_replace(PHP_EOL, '<br/>', $data['tswk']);//换行输出
        $data['nxwk'] = str_replace(PHP_EOL, '<br/>', $data['nxwk']);
        $data['description'] = str_replace(PHP_EOL, '<br/>', $data['description']);
        $data['add_uid'] = isset($_SESSION['dms_uid']) ? $_SESSION['dms_uid'] : $_SESSION['uid'];
        $data['add_user'] = isset($_SESSION['dms_username']) ? $_SESSION['dms_username'] : $_SESSION['username'];
        $data['add_date'] = date('Y-m-d');
        $data['add_time'] = date('Y-m-d H:i:s');
        return parent::insertData(self::$table2, $data);
    }

    /**
     * 获取指定运营周报
     * @param $field
     * @param $where
     * @param $order
     * @return mixed
     */
    public static function getWeeklyArray($field, $where, $order)
    {
        $result = parent::getResultOrder(self::$table2, $field, $where, $order);
        return $result;
    }

    /**
     * 获取单列周报
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getWeekly($field, $where)
    {
        $result = parent::getResultByField(self::$table2, $field, $where);
        $result['tswk'] = str_replace('<br/>', PHP_EOL, $result['tswk']);//html换行符转回textarea换行
        $result['nxwk'] = str_replace('<br/>', PHP_EOL, $result['nxwk']);
        $result['description'] = str_replace('<br/>', PHP_EOL, $result['description']);
        return $result;
    }

    /**
     * 修改周报
     * @param $data
     * @param $where
     * @return mixed
     */
    public static function updateWeekly($data, $where)
    {
        if (isset($data['tswk'])) $data['tswk'] = str_replace(PHP_EOL, '<br/>', $data['tswk']);//换行输出
        if (isset($data['nxwk'])) $data['nxwk'] = str_replace(PHP_EOL, '<br/>', $data['nxwk']);
        if (isset($data['description'])) $data['description'] = str_replace(PHP_EOL, '<br/>', $data['description']);
        if (isset($data['reply'])) {
            $data['reply_uid'] = isset($_SESSION['dms_uid']) ? $_SESSION['dms_uid'] : $_SESSION['uid'];
            $data['reply_user'] = isset($_SESSION['dms_username']) ? $_SESSION['dms_username'] : $_SESSION['username'];
            $data['reply_date'] = date('Y-m-d');
        }
        $result = parent::save(self::$table2, $data, $where);
        return $result;
    }

    /**
     * 删除周报
     * @param $where
     * @return mixed
     */
    public static function weeklyDel($where)
    {
        $result = parent::del(self::$table2, $where);
        return $result;
    }

    public static function doWeeklyAndProjects($res_weekly, $res_projects)
    {
        if (!empty($res_weekly) && !empty($res_projects)) {
            foreach ($res_weekly as $k => $weekly) {
                foreach ($res_projects as $projects) {
                    if ($weekly['pid'] == $projects['pid']) {
                        $res_weekly[$k]['project_num'] = $projects['project_num'];
                        $res_weekly[$k]['project_name'] = $projects['project_name'];
                    }
                }
            }
            return $res_weekly;
        } else {
            return array();
        }
    }


}